package com.yuq.sunrise.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yuq.sunrise.common.controller.AbstractController;
import com.yuq.sunrise.model.User;
import com.yuq.sunrise.service.UserService;
import com.yuq.sunrise.util.ResultMap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;

//@RestController //返回json,如需返回试图，则使用@Controller
@Controller
@RequestMapping("/user") //拦截路径，如不加改配置则访问方法url即可
public class UserController extends AbstractController {

    @Reference
    private UserService userService;

    /**
     * create by: yuq
     * description: 新增对象，原始方法
     * create time: 2019-06-08 19:40
     *
     * @param user
     * @return
     */
    @RequestMapping(value="/add", method= RequestMethod.POST)
    public ResultMap add(@RequestBody User user) {
        userService.save(user);
        return ResultMap.ok();
    }

    /**
     * create by: yuq
     * description: TODO
     * create time: 2019-06-08 19:40
     *
     * @param rowId
     * @return
     */
    @RequestMapping(value="/query/{rowId}", method= RequestMethod.GET)
    public ResultMap query(@PathVariable Integer rowId) {
        User user = userService.queryByPrimaryKey(rowId);
        return ResultMap.ok(user);
    }

    @RequestMapping(value="/queryAll", method= RequestMethod.GET)
    public ResultMap queryAll() {
        List<User> userList =  userService.queryAll();
        return ResultMap.ok(userList);
    }

    /**
     * create by: yuq
     * description: TODO
     * create time: 2019-06-08 19:40
     *
     * @param rowId,user
     * @return
     */
    @RequestMapping(value="/update/{rowId}", method= RequestMethod.PUT)
    public ResultMap updateUser(@PathVariable Long rowId, @RequestBody User user) {
        user.setRowId(rowId);
        userService.updateByPrimaryKey(user);
        return ResultMap.ok();
    }

    /**
     * create by: yuq
     * description: TODO
     * create time: 2019-06-08 19:40
     *
     * @param rowId
     * @return
     */
    @RequestMapping(value="/delete/{rowId}", method=RequestMethod.DELETE)
    public ResultMap deleteUser(@PathVariable Long rowId) {
        userService.deleteByPrimaryKey(rowId);
        return ResultMap.ok();
    }

    /****************************************** 以上为controller原始方法，一般无需变动 *****************************************/

    /**
     * create by: yuq
     * description: 登录
     * create time: 2019-06-23 15:29
     *
     * @param user
     * @return
     */
    @RequestMapping(value="/login", method= RequestMethod.GET)
    public ResultMap login(@RequestBody User user) {
        List<User> userList =  userService.queryAll();
        User returnUser = userService.queryOne(user);
        return ResultMap.ok(returnUser);
    }
}
